<template>
  <div>
    <el-button
      type="primary"
      size="mini"
      icon="el-icon-plus"
      @click="handleCreate"
      >创建博客</el-button
    >
    <el-table :data="blogs" border stripe style="width: 100%">
      <el-table-column type="index" width="50" label="#"></el-table-column>
      <el-table-column
        prop="blogId"
        label="博客id"
        width="60"
      ></el-table-column>
      <el-table-column prop="title" label="标题"></el-table-column>
      <el-table-column prop="category.title" label="分类">
        <template slot-scope="scope">
          {{ scope.row.category ? scope.row.category.title : "-" }}
        </template>
      </el-table-column>
      <el-table-column
        prop="tags"
        label="标签"
        :formatter="tagsFormatter"
      ></el-table-column>
      <el-table-column
        prop="updatedAt"
        label="更新于"
        width="155"
      ></el-table-column>
      <el-table-column
        prop="createdAt"
        label="创建于"
        width="155"
      ></el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            icon="el-icon-edit"
            @click="handleEdit(scope.row)"
            >编辑</el-button
          >
          <!-- 删除分类确认框 -->
          <el-popconfirm
            confirm-button-text="好的"
            cancel-button-text="不用了"
            icon="el-icon-info"
            icon-color="red"
            title="确认删除该博客吗？"
            style="margin-left: 10px"
            @confirm="handleDelete(scope.row)"
          >
            <el-button
              slot="reference"
              size="mini"
              type="danger"
              icon="el-icon-delete"
              class="btn-delete"
              >删除</el-button
            >
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      :page-sizes="[10, 20, 30]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
export default {
  name: "admin-blog",
  data() {
    return {
      total: 0, // 总数
      pageNum: 1, // 当前页码
      pageSize: 10, // 每页数量
      blogs: [] // 博客列表
    };
  },
  methods: {
    /**
     * 每页显示数量改变
     * @param val 改变后数量
     */
    handleSizeChange(val) {
      this.pageSize = val;
      this.getBlog();
    },
    /**
     * 当前页码改变
     * @param val 改变后页码
     */
    handleCurrentChange(val) {
      this.pageNum = val;
      this.getBlog();
    },
    /**
     * 获取博客列表
     */
    async getBlog() {
      let params = {
        pageNum: this.pageNum,
        pageSize: this.pageSize
      };
      let { data: res } = await this.$api.blog.getIndexBlog(params);
      this.total = res.data.total;
      this.blogs = res.data.blogList;
      window.scrollTo(0, 0);
    },
    /**
     * 过滤标签数据
     */
    tagsFormatter(row) {
      let tags = row.tags;
      // console.log(tags);
      if (tags.length === 0) return "-";
      tags = tags.map(item => {
        return item.name;
      });
      return tags.join(",");
    },
    /**
     * 跳转编辑博客
     */
    handleEdit(row) {
      const blogId = row.blogId;
      let routeUrl = this.$router.resolve({
        path: `/adminDJ/blog/edit/${blogId}`
      });
      window.open(routeUrl.href, "_blank");
    },
    /**
     * 删除博客
     */
    async handleDelete(row) {
      const blogId = row.blogId;
      const { data: res } = await this.$api.blog.deleteBlog(blogId);
      this.$message.success(res.msg);
      console.log(res);
      this.getBlog();
    },
    handleCreate() {
      window.open("#/adminDJ/blog/create", "_blank");
    }
  },
  created() {
    this.getBlog();
  }
};
</script>

<style lang="scss" scoped>
.el-pagination {
  margin-top: 20px;
}

.el-table {
  margin-top: 20px;
}
</style>
